package com.md.livingwidget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.md.basedpc.PHSizeUtils;
import com.md.livingwidget.R;
import com.md.livingwidget.toast.PHToast;

/**
 * 弹框工具类
 *
 * @author wanggq&xulp
 * @date :2019-11-25 17:46
 */
public class PHDialogUtils {
    /**
     * 弹出一个含有确定键的弹框
     *
     * @param content 弹框内容
     */
    public static Dialog showOne(Context mContext, String content) {
        return showOne(mContext, content, "", mContext.getString(R.string.common_btn_ok), null);
    }

    /**
     * 弹出一个含有确定键和标题的弹框
     *
     * @param content 弹框内容
     * @param title   标题内容
     */
    public static Dialog showOne(Context mContext, String content, String title) {
        return showOne(mContext, content, title, mContext.getString(R.string.common_btn_ok), null);
    }

    /***
     * 弹出一个可设置文字显示位置的弹框
     * @param content
     * @param title
     * @param gravity
     */
    public static Dialog showOne(Context mContext, String content, String title, GravityEnum gravity) {
        return showOne(mContext, content, title, gravity, mContext.getString(R.string.common_btn_ok), null);
    }

    /**
     * 弹出一个含有标题且自定义确认按钮文字的弹框
     *
     * @param content 弹框内容
     * @param title   弹框标题
     * @param btnText 按钮文字
     */
    public static void showOne(Context mContext, String content, String title, String btnText) {
        showOne(mContext, content, title, btnText, null);
    }

    /**
     * 弹出一个含有确定键的弹框 且包含按钮点击事件
     *
     * @param content       弹框内容
     * @param backLinstener 按钮点击事件回调 回调方法:oneBtn()
     */
    public static void showOne(Context mContext, String content, PHDialogBackLinstener backLinstener) {
        showOne(mContext, content, "", mContext.getString(R.string.common_btn_ok), backLinstener);
    }

    /**
     * 弹出一个含有确定键和标题的弹框 且包含按钮点击事件
     *
     * @param content       弹框内容
     * @param title         标题内容
     * @param backLinstener 按钮点击事件回调 回调方法:oneBtn()
     */
    public static void showOne(Context mContext, String content, String title, PHDialogBackLinstener backLinstener) {
        showOne(mContext, content, title, mContext.getString(R.string.common_btn_ok), backLinstener);
    }

    /**
     * 弹出一个含有标题且自定义确认按钮文字的弹框 且包含按钮点击事件
     *
     * @param content       弹框内容
     * @param title         弹框标题
     * @param btnText       按钮文字
     * @param backLinstener 按钮点击事件回调 回调方法:oneBtn()
     */
    public static MaterialDialog showOne(Context mContext, String content, String title, String btnText, PHDialogBackLinstener backLinstener) {
        return showOne(mContext, content, title, btnText, backLinstener, GravityEnum.CENTER);
    }


    /***
     * 弹出一个含有标题且自定义确认按钮文字和设置文字位置的弹框 且包含按钮点击事件
     * @param content               弹框内容
     * @param title                 弹框标题
     * @param gravity               文字位置
     * @param btnText               按钮文字
     * @param backLinstener         按钮点击事件回调 回调方法:oneBtn()
     */
    public static Dialog showOne(Context mContext, String content, String title, GravityEnum gravity, String btnText, PHDialogBackLinstener backLinstener) {
        return showOne(mContext, content, title, btnText, backLinstener, gravity);
    }

    public static MaterialDialog showOne(Context mContext, String content, String title, String btnText, PHDialogBackLinstener backLinstener, GravityEnum gravityEnum) {
        content = content.replace("\r\n", "<br>");
        MaterialDialog.Builder builder = new MaterialDialog.Builder(mContext);
        builder.positiveText(btnText)
                .contentGravity(gravityEnum)
                .onPositive((materialDialog, dialogAction) -> {
                    if (backLinstener != null)
                        backLinstener.oneBtn();
                })
                .positiveColor(mContext.getResources().getColor(R.color.ui_dialog_btn_text))
                .cancelable(false)
                .canceledOnTouchOutside(false);
        if (TextUtils.isEmpty(content) || TextUtils.isEmpty(title)) {
            if (!TextUtils.isEmpty(content)) {
                builder.title(content);
            } else if (!TextUtils.isEmpty(title)) {
                builder.title(title);
            }
        } else {
            builder.content(Html.fromHtml(content));
            builder.title(title);
        }
        return builder.show();
    }

    /**
     * 弹出一个含有两个按钮的对话框
     *
     * @param content 弹框内容
     */
    public static Dialog showTwo(Context mContext, String content) {
        return showTwo(mContext, content, "", mContext.getString(R.string.common_btn_cancel), mContext.getString(R.string.common_btn_ok), true, null);
    }

    /**
     * 弹出一个含有两个按钮的对话框
     *
     * @param content 弹框内容
     * @param title   弹框标题
     */
    public static Dialog showTwo(Context mContext, String content, String title) {
        return showTwo(mContext, content, title, mContext.getString(R.string.common_btn_cancel), mContext.getString(R.string.common_btn_ok), true, null);
    }

    /**
     * 弹出一个含有两个按钮的对话框 且包含按钮点击事件
     *
     * @param content       弹框内容
     * @param backLinstener 按钮点击事件回调 左边按钮回调:leftBtn();右边按钮回调:rightBtn
     */
    public static Dialog showTwo(Context mContext, String content, PHDialogBackLinstener backLinstener) {
        return showTwo(mContext, content, "", mContext.getString(R.string.common_btn_cancel), mContext.getString(R.string.common_btn_ok), true, backLinstener);
    }

    /**
     * 弹出一个含有两个按钮的对话框 且包含按钮点击事件
     *
     * @param content       弹框内容
     * @param backLinstener 按钮点击事件回调 左边按钮回调:leftBtn();右边按钮回调:rightBtn
     */
    public static Dialog showTwo(Context mContext, String content, boolean isCanKeyBack, PHDialogBackLinstener backLinstener) {
        return showTwo(mContext, content, "", mContext.getString(R.string.common_btn_cancel), mContext.getString(R.string.common_btn_ok), isCanKeyBack, backLinstener);
    }

    /**
     * @param content
     * @param title
     * @param backLinstener
     */
    public static Dialog showTwo(Context mContext, String content, String title, PHDialogBackLinstener backLinstener) {
        return showTwo(mContext, content, title, mContext.getString(R.string.common_btn_cancel), mContext.getString(R.string.common_btn_ok), true, backLinstener);
    }

    /**
     * 弹出一个含有两个按钮的对话框 且包含按钮点击事件
     *
     * @param content       弹框内容
     * @param left          左键文字
     * @param right         右键文字
     * @param backLinstener 按钮点击事件回调 左边按钮回调:leftBtn();右边按钮回调:rightBtn
     */
    public static Dialog showTwo(Context mContext, String content, String left, String right, PHDialogBackLinstener backLinstener) {
        return showTwo(mContext, content, "", left, right, true, backLinstener);
    }

    public static MaterialDialog showTwo(Context mContext, String content, String title, String left, String right, PHDialogBackLinstener backLinstener) {
        return showTwo(mContext, content, title, left, right, true, backLinstener);
    }

    /**
     * 弹出一个含有两个按钮的对话框 且包含按钮点击事件
     *
     * @param content       弹框内容
     * @param title         弹框标题
     * @param left          左键文字
     * @param right         右键文字
     * @param backLinstener 按钮点击事件回调 左边按钮回调:leftBtn();右边按钮回调:rightBtn
     */
    public static MaterialDialog showTwo(Context mContext, String content, String title, String left, String right, boolean isCanKeyBack, PHDialogBackLinstener backLinstener) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(mContext);
        builder.canceledOnTouchOutside(false)
                .cancelable(isCanKeyBack)
                .positiveText(left)
                .neutralText(right)
                .onPositive((materialDialog, dialogAction) -> {
                    if (backLinstener != null) {
                        backLinstener.leftBtn();
                    }
                })
                .neutralColor(mContext.getResources().getColor(R.color.ui_dialog_btn_text))
                .onNeutral((materialDialog, dialogAction) -> {
                    if (backLinstener != null) {
                        backLinstener.rightBtn();
                    }
                });
        if (TextUtils.isEmpty(content) || TextUtils.isEmpty(title)) {
            if (!TextUtils.isEmpty(content)) {
                builder.title(content);
            } else if (!TextUtils.isEmpty(title)) {
                builder.title(title);
            }
        } else {
            builder.content(Html.fromHtml(content));
            builder.title(title);
        }
        return builder.show();
    }

    public static Dialog showThree(Context mContext, String content, String left, String right, String middle, PHDialogBackLinstener backLinstener) {
        return new MaterialDialog.Builder(mContext)
                .canceledOnTouchOutside(false)
                .content(content)
                .positiveText(left)
                .negativeText(middle)
                .neutralText(right)
                .positiveColor(mContext.getResources().getColor(R.color.ui_dialog_btn_text))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        if (backLinstener != null) {
                            backLinstener.leftBtn();
                        }
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        if (backLinstener != null) {
                            backLinstener.middleBtn();
                        }
                    }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        if (backLinstener != null) {
                            backLinstener.rightBtn();
                        }
                    }
                })
                .show();
    }

    public static Dialog showThree(Context mContext, String title, String content, String left, String right, String middle, PHDialogBackLinstener backLinstener) {
        return new MaterialDialog.Builder(mContext)
                .canceledOnTouchOutside(false)
                .title(title)
                .content(content)
                .positiveText(left)
                .negativeText(middle)
                .neutralText(right)
                .positiveColor(mContext.getResources().getColor(R.color.ui_dialog_btn_text))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        if (backLinstener != null) {
                            backLinstener.leftBtn();
                        }
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        if (backLinstener != null) {
                            backLinstener.middleBtn();
                        }
                    }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        if (backLinstener != null) {
                            backLinstener.rightBtn();
                        }
                    }
                })
                .show();
    }

    /**
     * 打开一个有返回值的输入框对话框 且有回调事件
     *
     * @param content       下面的提示内容
     * @param backLinstener 回调返回   调用方法:rightBtn(String res)
     */
    public static Dialog showEdit(Context mContext, String content, PHDialogBackLinstener backLinstener) {
        return showEdit(mContext, content, "", mContext.getString(R.string.common_btn_ok), mContext.getString(R.string.common_btn_cancel), backLinstener, -1, false, "");
    }

    /**
     * 打开一个有返回值的输入框对话框 且有回调事件
     *
     * @param content       下面的提示内容
     * @param title         弹框标题
     * @param backLinstener 回调返回   调用方法:rightBtn(String res)
     */
    public static Dialog showEdit(Context mContext, String content, String title, PHDialogBackLinstener backLinstener) {
        return showEdit(mContext, content, title, mContext.getString(R.string.common_btn_ok), mContext.getString(R.string.common_btn_cancel), backLinstener, -1, false, "");
    }

    /**
     * 打开一个有返回值的输入框对话框 且有回调事件
     *
     * @param content       下面的提示内容
     * @param title         弹框标题
     * @param backLinstener 回调返回   调用方法:rightBtn(String res)
     */
    public static Dialog showEdit(Context mContext, String content, String title, PHDialogBackLinstener backLinstener, boolean isIntOnly) {
        return showEdit(mContext, content, title, mContext.getString(R.string.common_btn_ok), mContext.getString(R.string.common_btn_cancel), backLinstener, -1, isIntOnly, "");
    }

    /**
     * 打开一个有返回值的输入框对话框 且有回调事件
     *
     * @param content       下面的提示内容
     * @param title         弹框标题
     * @param backLinstener 回调返回   调用方法:rightBtn(String res)
     */
    public static Dialog showEdit(Context mContext, String content, String title, PHDialogBackLinstener backLinstener, int length, boolean isIntOnly, String editText) {
        return showEdit(mContext, content, title, mContext.getString(R.string.common_btn_ok), mContext.getString(R.string.common_btn_cancel), backLinstener, length, isIntOnly, editText);
    }

    /**
     * 打开一个有返回值的输入框对话框 且有回调事件
     *
     * @param content       下面的提示内容
     * @param title         弹框标题
     * @param left          左键文字
     * @param right         右键文字
     * @param backLinstener 回调返回   调用方法:rightBtn(String res)
     */
    public static Dialog showEdit(Context mContext, String content, String title, String left, String right, PHDialogBackLinstener backLinstener, int length, boolean isIntOnly, String editText) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(mContext);
        builder
//                .inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME | InputType.TYPE_TEXT_FLAG_CAP_WORDS)
//                .widgetColor(mContext.getResources().getColor(R.color.color_cacaca))
                .canceledOnTouchOutside(false)
                .positiveText(right)
                .negativeText(left)

                .contentMaxHeight(PHSizeUtils.mm2px(mContext, 120))
                .negativeColor(mContext.getResources().getColor(R.color.ui_dialog_btn_text))
                .alwaysCallInputCallback() // this forces the callback to be invoked with every input change
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        materialDialog.dismiss();
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog materialDialog, DialogAction dialogAction) {
                        if (backLinstener != null) {
                            backLinstener.leftBtn(materialDialog.getInputEditText().getText().toString());
                        }
                    }
                })
                .input(editText, "", true, new MaterialDialog.InputCallback() {
                    @Override
                    public void onInput(MaterialDialog dialog, CharSequence input) {
                        View t = dialog.findViewById(R.id.md_minMax);
                        t.setVisibility(View.INVISIBLE);
                    }
                });
        if (TextUtils.isEmpty(content) || TextUtils.isEmpty(title)) {
            if (!TextUtils.isEmpty(content)) {
                builder.title(content);
            } else if (!TextUtils.isEmpty(title)) {
                builder.title(title);
            }
        } else {
            builder.content(Html.fromHtml(content));
            builder.title(title);
        }
        MaterialDialog mg = builder.build();
        View t = mg.findViewById(R.id.md_minMax);
        t.setVisibility(View.INVISIBLE);
        mg.getInputEditText().setFilters(new InputFilter[]{new InputFilter.LengthFilter(length == -1 ? 200 : length)});
        mg.show();
        return mg;
    }

    public static Dialog showCustom(Context mContext, String content, String title, String left, String right, boolean isCanKeyBack, PHDialogBackLinstener backLinstener) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(mContext);
        final LayoutInflater inflater = LayoutInflater.from(mContext);
        View rootView = inflater.inflate(R.layout.ph_dialog_check, null);
        CheckBox checkBox = rootView.findViewById(R.id.ph_dialog_text);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                PHToast.showSingleToast(mContext, isChecked ? "选中" : "取消");
            }
        });
        builder.canceledOnTouchOutside(false)
                .customView(rootView, false)
                .cancelable(isCanKeyBack)
                .positiveText(left)
                .neutralText(right)
                .onPositive((materialDialog, dialogAction) -> {
                    if (backLinstener != null) {
                        backLinstener.leftBtn();
                    }
                })
                .neutralColor(mContext.getResources().getColor(R.color.ui_dialog_btn_text))
                .onNeutral((materialDialog, dialogAction) -> {
                    if (backLinstener != null) {
                        backLinstener.rightBtn();
                    }
                });
        if (TextUtils.isEmpty(content) || TextUtils.isEmpty(title)) {
            if (!TextUtils.isEmpty(content)) {
                builder.title(content);
            } else if (!TextUtils.isEmpty(title)) {
                builder.title(title);
            }
        } else {
            builder.content(Html.fromHtml(content));
            builder.title(title);
        }
        return builder.show();
    }
}
